package com.store.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.common.common.BaseResponse;
import com.common.common.ErrorCode;
import com.common.common.ResultUtils;

import com.common.controller.AbstractTypedController;
import com.store.entities.Business;
import com.store.entities.Store;
import com.store.service.BusinessServiceImpl;
import com.store.service.StoreServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author fu
 */
@Component
@RestController
@RequestMapping("business")
@Slf4j
@CrossOrigin
@Tag(name = "商家")
public class BusinessController extends AbstractTypedController<Business, Integer> {
    @Resource
    BusinessServiceImpl businessService;

    BusinessController(BusinessServiceImpl muSvc ) {
        this.businessService = muSvc;
        this.svcContext = muSvc;
    }

    /**
     * 商家分页查询
     * 改善接口中资源定义和被流控降级之后的处理方法
     * 添加Bean,value定义资源，blockHandler定义被流控降级之后的处理方法
     * blockhandlerClass定义处理方法所在的类,blockHandler定义处理方法
     * fallback定义业务异常处理方法,当接口出现了异常，
     * exceptionToIgnore定义不需要处理的异常
     * @param pageSize
     * @return
     */
    @GetMapping("/pagesearch/{startPage}/{pageSize}")
    @Operation(summary = "商家分页查询")
    @SentinelResource(value = "pageBusiness",blockHandler = "blockHandlerForPageBusiness")
    public BaseResponse<List<Business>> pageBusiness( @PathVariable Integer startPage, @PathVariable Integer pageSize ) {
        List<Business> businesses;
        try {
            businesses=businessService.pageSearch(startPage,pageSize);
        }catch (Exception e) {
            return ResultUtils.error(ErrorCode.NULL_ERROR,"服务器错误");
        }
        return ResultUtils.success(businesses);
    }

    /*
        * 资源定义和被流控降级之后的处理方法
        * 返回值河源方法保持一致，包含原方法的参数
     */
    public BaseResponse<List<Business>> blockHandlerForPageBusiness( @PathVariable Integer startPage, @PathVariable Integer pageSize,Throwable e ) {
        log.error("blockHandlerForPageBusiness:{}",e);
        return ResultUtils.error(ErrorCode.NULL_ERROR,"流控");
    }

}
